home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0793 / HEXPCHAR.ASM < prev    next >
Assembly Source File  |  1993-08-01  |  4KB  |  107 lines

  1. ;─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. ;Msg  : 67 of 157
  3. ;From : Wilbert van Leijen                  2:281/256.14         13 Jul 93  22:05
  4. ;To   : Charles Lumia                       1:150/320.0
  5. ;Subj : Mouse pointers
  6. ;────────────────────────────────────────────────────────────────────────────────
  7. ;06 Jul 93, Charles Lumia writes to All:
  8. ;
  9. ; CL> How can I allow it to accept any # of variables and any type to pass them
  10. ; CL> to writeln, like the real writeln? * SPITFIRE v3.3
  11. ;
  12. ;That's only feasible by using the C calling convention, or in external
  13. ;assembler code using tricky code that fiddles with the stack.  The first is not
  14. ;an option in Borland Pascal (it is, however, supported in TopSpeed Pascal).
  15. ;The latter assumes having mastered the Zen of assembly language.  Nevertheless,
  16. ;the rules of the Pascal calling convention still apply: called routine must
  17. ;remove arguments from the stack - or kaboom!
  18. ;
  19. ;Here follows an example of an ASM module that exports three routines, all
  20. ;sharing a common exit point.  Two of the three, HexByte and HexWord, allocate
  21. ;10 bytes on the stack (6 bytes thereof are parameters, 4 bytes make up the
  22. ;return address when called Far), whereas HexLongInt pushes 12 bytes.  On exit,
  23. ;in each case the value of the SP register is properly restored.
  24. ;
  25. ;Sample usage:
  26. ;
  27. ;Var
  28. ;  buf : Array[0..9] of Char;     { PChar scratch pad }
  29. ;
  30. ;Begin  WriteLn(HexLongInt(MaxLongInt, buf))); end.
  31. ;
  32. ;HEXPCHAR.ASM:
  33. ;
  34. ;  Written by Wilbert van Leijen.  Released to the public domain.
  35. ;  Use TASM to assemble:  TASM -m2 -zi HEXPCHAR
  36. ;
  37.            MODEL   LARGE       ; don't specify a language!
  38. .CODE
  39.            PUBLIC  HexByte, HexWord, HexLongInt
  40.  
  41. ;  Function HexLongInt(l : LongInt; Result : PChar) : PChar; Far;
  42. ;  Function HexWord(w : Word; Result : PChar) : PChar; Far;
  43. ;  Function HexByte(b : Byte; Result : PChar) : PChar; Far;
  44.  
  45. HexDigitWord PROC  Near
  46.            PUSH    AX
  47.            XCHG    AH, AL
  48.            CALL    HexDigitByte
  49.            POP     AX
  50.  
  51. HexDigitByte:
  52.            PUSH    AX
  53.            MOV     CL, 4
  54.            SHR     AL, CL
  55.            CALL    HexDigitNibble
  56.            POP     AX
  57.            AND     AL, 00001111b
  58.  
  59. HexDigitNibble:
  60.            ADD     AL, 90h
  61.            DAA
  62.            ADC     AL, 40h
  63.            DAA
  64.            STOSB
  65.            RETN
  66. HexDigitWord ENDP
  67.  
  68. HexLongInt PROC
  69.            ARG     ResultString : DWord, LoWord, HiWord : Word = ArgSize
  70.  
  71.            MOV     DX, Offset HexDigitWord
  72.            STC                         ;  CF = display two words
  73.            JMP     Short @@2
  74.  
  75. HexWord:   MOV     DX, Offset HexDigitWord
  76.            JMP     Short @@1
  77.  
  78. HexByte:   MOV     DX, Offset HexDigitByte
  79.  
  80. @@1:       CLC
  81. @@2:       PUSH    BP
  82.            MOV     BP, SP
  83.            LES     DI, ResultString
  84.            PUSH    DI
  85.            MOV     BX, ArgSize-2
  86.            JNC     @@3
  87.  
  88.            MOV     AX, HiWord
  89.            CALL    DX
  90.            INC     BX                  ;  remove 2 bytes more from stack
  91.            INC     BX
  92.  
  93. @@3:       MOV     AX, LoWord
  94.            CALL    DX
  95.  
  96.            XOR     AL, AL              ;  Trailing null
  97.            STOSB
  98.            POP     AX                  ;  Return pointer to result string
  99.            MOV     DX, ES
  100.            POP     BP
  101.            POP     DI  ES              ;  Save return address
  102.            ADD     SP, BX              ;  Remove parameters from the stack
  103.            PUSH    ES  DI              ;  Restore return address
  104.            RET
  105. HexLongInt ENDP
  106.  
  107.            END